package com.augn.service.impl;

import com.augn.entity.Order;
import com.augn.entity.Order;
import com.augn.entity.Result;
import com.augn.mapper.OrderMapper;
import com.augn.mapper.OrderMapper;
import com.augn.service.IOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author augn
 * @since 2022-09-22
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private OrderMapper mapper;

    // 新增一个订单
    @Override
    public Result<?> insertOneOrder(Order order) {
        int insert = mapper.insert(order);
        if(insert==0){
            return Result.error("-1","新增失败");
        } else{
            return Result.success(insert);
        }
    }

    //删除一个订单
    @Override
    public Result<?> deleteOneOrder(Integer id) {
        Order order = new Order();
        order.setOrderId(id);
        order.setIsDelete(1);
        int delete = mapper.updateById(order);
        if(delete==0){
            return Result.error("-1","删除失败");
        } else{
            return Result.success(delete);
        }
    }

    //更新一个订单
    @Override
    public Result<?> updateOneOrder(Order order) {
        int update = mapper.updateById(order);
        if(update==0){
            return Result.error("-1","更新失败");
        } else{
            return Result.success(update);
        }
    }

    //获取一个订单
    @Override
    public Result<?> getOneOrder(Integer id) {
        Order order = mapper.selectById(id);
        if(order==null){
            return Result.error("-1","无此商品");
        } else{
            return Result.success(order);
        }
    }

    //获取符合条件的订单
    @Override
    public Result<?> getPartOrder(Order order,Integer pageNum,Integer pageSize) {
        QueryWrapper<Order> wrapper= new QueryWrapper<>();
        if(order.getName()!=null){
            wrapper.like("good_name",order.getName());
        }
        if (order.getGoodsId()!=null) {
            wrapper.eq("goods_id",order.getGoodsId());
        }
        if (order.getUserId()!=null) {
            wrapper.eq("user_id",order.getUserId());
        }

        long pages ,total;
        List<Order> orderList;
        Map<String,Object> ret = new HashMap<>();
        if((pageNum==null&&pageSize==null)||(pageNum==null&&pageSize!=null)){
            orderList =  mapper.selectList(wrapper);
            ret.put("orderList",orderList);
        } else {
            if(pageSize==null){
                pageSize=20;
            }
            Page<Order> orderPage = new Page<>(pageNum , pageSize);
            IPage<Order> orderIPage = mapper.selectPage(orderPage , wrapper);
            orderList = orderIPage.getRecords();
            pages = orderIPage.getPages();
            total = orderIPage.getTotal();
            ret.put("orderList",orderList);
            ret.put("pages",pages);
            ret.put("total",total);
        }

        if(orderList.isEmpty()){
            return Result.error("-1","没有符合此条件的订单");
        } else{
            return Result.success(ret);
        }
    }
}
